package process

import (
	_ "encoding/binary"
	"encoding/json"
	"fmt"
	"manyuser/client/utils"
	"manyuser/common/message"
	"net"
)

type UserProcess struct{}

func (this *UserProcess) Login(userId int, userPwd string) (err error) {
	conn, err := net.Dial("tcp", "localhost:8889")
	defer conn.Close()
	if err != nil {
		fmt.Println("client.Dial is wrong,", err)
		return
	}
	//发送消息
	var mes message.Message
	mes.Type = message.LoginMesType

	var loginMes message.LoginMes
	loginMes.UserId = userId
	loginMes.UserPwd = userPwd
	data, err := json.Marshal(loginMes)
	if err != nil {
		fmt.Println("json.Marshal is wrong,", err)
		return
	}

	mes.Data = string(data)

	data, err = json.Marshal(mes)
	if err != nil {
		fmt.Println("json.Marshal is wrong,", err)
		return
	}
		ts:=&utils.Transfer{Conn:conn,}
	err = ts.WritePkg(data)
	if err != nil {
		fmt.Println("client writePkg(conn,data) wrong,", err)
		return
	}
	fmt.Printf("client send message length is:%d,body is:%s\n", len(data), string(data))

	mes, err = ts.ReadPkg()
	if err != nil {
		fmt.Println("client =readPkg(conn) wrong,", err)
		return
	}
	//分析消息类型
	var loginResMes message.LoginResMes
	err = json.Unmarshal([]byte(mes.Data), &loginResMes)
	if err != nil {
		fmt.Println("client json.UnMarshal([]byte(mes.Data),loginResMes) wrong,", err)
		return
	}
	if loginResMes.Code == 200 {
		//CurUser初始化
		CurUser.Conn=conn
		CurUser.UserId=userId
		CurUser.UserStatus=message.UserOnline

		//在线用户列表初始
		fmt.Println("Online person is the,",loginResMes.UsersId)
		for _,id :=range loginResMes.UsersId {
			user:=&message.User{
				UserId:id,
				UserStatus:message.UserOnline,
			}
			onlineUsers[id]=user
		}

		go serverProcessMes(conn)
		ShowMeu()

	} else  {
		fmt.Println(loginResMes.Error)
	}
	return

}

func (this *UserProcess) Register(userId int, userPwd string,userName string) (err error) {
	conn, err := net.Dial("tcp", "localhost:8889")
	defer conn.Close()
	if err != nil {
		fmt.Println("client.Dial is wrong,", err)
		return
	}
	//发送消息
	var mes message.Message
	mes.Type = message.RegisterMesType

	var registerMes message.RegisterMes
	registerMes.User.UserId = userId
	registerMes.User.UserPwd = userPwd
	registerMes.User.UserName = userName

	data, err := json.Marshal(registerMes)
	if err != nil {
		fmt.Println("json.Marshal is wrong,", err)
		return
	}

	mes.Data = string(data)

	data, err = json.Marshal(mes)
	if err != nil {
		fmt.Println("json.Marshal is wrong,", err)
		return
	}
		ts:=&utils.Transfer{Conn:conn,}
	err = ts.WritePkg(data)
	if err != nil {
		fmt.Println("client writePkg(conn,data) wrong,", err)
		return
	}
	fmt.Printf("client send message length is:%d,body is:%s\n", len(data), string(data))

	mes, err = ts.ReadPkg()
	if err != nil {
		fmt.Println("client =readPkg(conn) wrong,", err)
		return
	}
	//分析消息类型
	var registerResMes message.RegisterResMes
	err = json.Unmarshal([]byte(mes.Data), &registerResMes)
	if err != nil {
		fmt.Println("client json.UnMarshal([]byte(mes.Data),loginResMes) wrong,", err)
		return
	}
	if registerResMes.Code == 200 {
		fmt.Println("register success,login again")
	} else  {
		fmt.Println( registerResMes.Error)
	}
	
	return

}